package com.example.mall.auth.svc.service;

import com.example.mall.auth.svc.mapper.AuthAccountMapper;
import com.example.mall.auth.svc.model.AuthAccount;
import com.example.mall.auth.svc.model.AuthUser;
import com.example.mall.common.service.AbstractService;
import com.example.mall.user.api.dto.UserDTO;
import com.example.mall.user.api.service.IUserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @author Mickey
 * @since 2022/4/10
 **/
@Service
@AllArgsConstructor
public class AuthAccountService extends AbstractService {

    private final AuthAccountMapper authAccountMapper;
    private final IUserService userService;

    public void create(String uniqueId) {
        creatorFor(authAccountMapper)
                .instance(AuthAccount::new)
                .update(authAccount -> authAccount.setUniqueId(uniqueId))
                .call();
    }

    public void load(String username) {
        UserDTO userDTO = userService.loadByUniqueId(username);
        System.out.println(userDTO);

    }

    public AuthUser get(String uniqueId) {
        return authAccountMapper.selectByUniqueId(uniqueId);
    }

    public AuthUser getOrCreate(String uniqueId) {
        return Optional
                .ofNullable(authAccountMapper.selectByUniqueId(uniqueId))
                .orElseGet(() -> {
                    create(uniqueId);
                    return getOrCreate(uniqueId);
                });
    }


}
